<?php

namespace app\shop\controller;

use think\Controller;

class Address extends Controller
{
    //构造函数
    public function __construct()
    {
        parent::__construct();
        //加载模型
        $this->userModel = model('common/User/User');
        $this->bannerModel = model('common/User/Banner');
        $this->shopModel = model('common/User/Shop');
        $this->cartModel = model('common/User/Cart');
        $this->addressModel = model('common/User/Address');
        $this->regionModel = model('common/User/Region');
    }


    // 新增地址
    public function addAddress(){
      if($this->request->isPost()){
        // halt(11);
        $uid = (int)$this->request->param('uid');
        $addressDetail = $this->request->param('addressDetail');
        $areaCode = $this->request->param('areaCode');
        $tel = $this->request->param('tel');
        $isDefault = $this->request->param('isDefault');
        // halt($isDefault);
        $name = $this->request->param('name');
        // 查询是否为真正的用户
        $isUserRes =  $this->userModel->where('id',$uid)->select();
        // halt($isUserRes);
        if($isUserRes){
          $addressData = [
            'uid'=>$uid,
            'name'=>$name,
            'tel'=>$tel,
            'district_code'=>$areaCode,
            'address'=>$addressDetail,
            'isDefault'=>$isDefault,
          ];
          // 查询有没有这个城市编码有没有在这
          $regionRes =  $this->regionModel->where('code',$areaCode)->value('parentpath');
          // halt($regionRes);
          if ($regionRes) {
            $regionCode = explode(',',$regionRes);
            // halt($regionCode);
            if(!empty($regionCode[0])){
              $addressData['province']=$regionCode[0];
            }
            if(!empty($regionCode[1])){
              $addressData['city']=$regionCode[1];
            }
            if(!empty($regionCode[2])){
              $addressData['district']=$regionCode[2];
            }
          }
          
          // 如果等于1，则是设置为默认，要先查一整个表有没有1
          if($isDefault==1){
            // $addressData['district_code']=$areaCode;
            $isDeData = [
              'isDefault'=>0
            ];
            // $isDefaultRes = $this->addressModel->where('is_default',1)->saveAll('is_default',0);
            $isDefaultRes = $this->addressModel->where('isDefault',1)->update($isDeData);
            // halt($isDefaultRes,111);
          }
          // halt($addressData);
          $addRes = $this->addressModel->save($addressData);
          $this->success('新增地址成功',null,$addressData);
          exit;
        }else{
          $this->error('用户不存在');
          exit;
        }
      }
    }

    // 查询地址
    public function getAddress(){
      if($this->request->isPost()){
        $uid = (int)$this->request->param('uid');
        $mobile = $this->request->param('mobile');

        if(!empty($uid) && !empty($mobile)){

          $uData = [
            'id'=>$uid,
            'mobile'=>$mobile
          ];
          $isUserRes = $this->userModel->where($uData)->select();
          if ($isUserRes) {
            $getAddressList = $this->addressModel->where('uid',$uid)->select();
            $this->success('查询地址列表成功',null,$getAddressList);
            exit;
          }else{
            $this->error('用户不合法');
            exit;
          }

        }else{
          $this->error('用户ID和手机号不能为空！');
          exit;
        }
      }
    }

    // 获取之前用户填写的地址
    public function getEditAddress(){
      if($this->request->isPost()){
        $uid = $this->request->param('uid');
        $addressId = $this->request->param('addressId');
        $isUserRes =  $this->userModel->where('id',$uid)->select();
        if($isUserRes){
          $selectAddress = $this->addressModel->where('id',$addressId)->find();
          // halt($selectAddress['name']);
          $addressData = [
            'name'=>$selectAddress['name'],
            'areaCode'=>$selectAddress['district_code'],
            'tel'=>$selectAddress['tel'],
            'addressDetail'=>$selectAddress['address'],
          ];

          if($selectAddress['isDefault']){
            $addressData['isDefault']=true;
          }else{
            $addressData['isDefault']=false;
          }
          $this->success('查询数据成功',null,$addressData);
          exit;
        }
      }
    }

    // 修改操作
    public function saveEditAddress(){
      if($this->request->isPost()){
        $uid = $this->request->param('uid');
        $addressId = $this->request->param('addressId');
        $addressDetail = $this->request->param('addressDetail');
        $areaCode = $this->request->param('areaCode');
        $tel = $this->request->param('tel');
        $isDefault = $this->request->param('isDefault');
        $name = $this->request->param('name');

        $isUserRes =  $this->userModel->where('id',$uid)->select();
        if($isUserRes){
          // 查询对应的地址的数据
          $selectData = [
            'uid'=>$uid,
            'id'=>$addressId,
          ];

          // 修改的数据
          $editData = [
            'address'=>$addressDetail,
            'name'=>$name,
            'isDefault'=>$isDefault,
            'tel'=>$tel,
            'district_code'=>$areaCode
          ];
          // 查询有没有这个城市编码有没有在这
          $regionRes =  $this->regionModel->where('code',$areaCode)->value('parentpath');
          // halt($regionRes);
          if ($regionRes) {
            $regionCode = explode(',',$regionRes);
            // halt($regionCode);
            if(!empty($regionCode[0])){
              $editData['province']=$regionCode[0];
            }
            if(!empty($regionCode[1])){
              $editData['city']=$regionCode[1];
            }
            if(!empty($regionCode[2])){
              $editData['district']=$regionCode[2];
            }
          }
          // 如果等于1，则是设置为默认，要先查一整个表有没有1
          if($isDefault==1){
            // $addressData['district_code']=$areaCode;
            $isDeData = [
              'isDefault'=>0
            ];
            // $isDefaultRes = $this->addressModel->where('is_default',1)->saveAll('is_default',0);
            $isDefaultRes = $this->addressModel->where('isDefault',1)->update($isDeData);
            // halt($isDefaultRes,111);
          }

          $saveRes = $this->addressModel->where($selectData)->update($editData);
          $this->success('修改成功',null);
          exit;

        }
      }
    }

    public function delEditAddress(){
       if($this->request->isPost()){
        $uid = $this->request->param('uid');
        $addressId = $this->request->param('addressId');
        $isUserRes =  $this->userModel->where('id',$uid)->select();
        if($isUserRes){
          $delData = [
            'id'=>$addressId,
            'uid'=>$uid
          ];
          $delRes = $this->addressModel->destroy($delData);
          $this->success('删除成功',null);
          exit;
        }
        
       }
    }

}

?>

